# ----------------------------------------------------------------------
# Alcatel.7324RU.get_vlans
# ----------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------

# Python modules
import re

# NOC modules
from noc.core.script.base import BaseScript
from noc.sa.interfaces.igetvlans import IGetVlans


class Script(BaseScript):
    name = "Alcatel.7324RU.get_vlans"
    interface = IGetVlans

    rx_vlan = re.compile(
        r"\s*(?P<vid>\d+)"
        r"\s*(?P<vname>[A-Za-z0-9\-\.]+)\n"
        r"(([ 0-9]+)\n)?[ ]+(?P<vstatus>enabled|disabled)[ 0-9]+\n"
        r"([ \-xnf]+)\n[ ]+(?P<portmask>[\-tu]+)\s*"
        r"(?P<uplinkmask>[\-tu]*)",
        re.MULTILINE | re.IGNORECASE,
    )

    def execute(self):
        v = self.cli("switch vlan show *")
        r = []
        for match in self.rx_vlan.finditer(v):
            if match.group("vstatus") == "enabled":
                r += [{"vlan_id": int(match.group("vid")), "name": match.group("vname")}]
        return r
